<template>
	<view class="">
		<view class="" style="padding-bottom: 120rpx;">
			<view class="mainpadding ffffff">
				<view class="ershiba nofonweight xiaohei">散场主图</view>
				<view class="margin_top shangc" v-if="!image" @click="uploadImg(1)">
					<image src="../static/image/system/shangchuan.png" mode=""></image>
				</view>
				<view class="margin_top shangc" v-if="image" @click="uploadImg(1)">
					<image :src="image_text" mode=""></image>
				</view>
				<view class="ershiba nofonweight xiaohei margin_top">散场轮播图（{{images_text.length}}/5）</view>
				<view class="flexleft flex_wrap">
					<view class="shilitu margin_top margin_right2 dingwei" v-for="(item,index) in images_text"
						:key="index">
						<image :src="item" mode=""></image>
						<view class="shanchu" @click="delindex(index,images_text,images)">
							<u-icon name="close-circle-fill"></u-icon>
						</view>
					</view>
					<view class="margin_top shangc margin_right2" v-if="images.length<5"
						@click="uploadImg(5,images,images_text)">
						<image src="../static/image/system/shangchuan.png" mode=""></image>
					</view>
				</view>
				<view class="ershiba nofonweight xiaohei margin_top">散场名称</view>
				<view class="margin_top">
					<u--input placeholder="请输入散场名称" border="surround" v-model="name"></u--input>
				</view>
				<view class="ershiba nofonweight xiaohei margin_top">散场详情图（{{content_images_text.length}}/5）</view>
				<view class="flexleft flex_wrap">
					<view class="shilitu margin_top margin_right2 dingwei" v-for="(item,index) in content_images_text"
						:key="index">
						<image :src="item" mode=""></image>
						<view class="shanchu" @click="delindex(index,content_images_text,content_images)">
							<u-icon name="close-circle-fill"></u-icon>
						</view>
					</view>
					<view class="margin_top shangc margin_right2" v-if="content_images.length<5"
						@click="uploadImg(5,content_images,content_images_text)">
						<image src="../static/image/system/shangchuan.png" mode=""></image>
					</view>
				</view>
			</view>
			<view class="mainpadding ffffff margin_top1">
				<view class="xiahuaxian flexbetween margin_top">
					<view class="ershiba nofonweight xiaohei">价格</view>
					<input class="textright" type="digit" placeholder="输入价格" v-model="price">
				</view>
				<view class="xiahuaxian flexbetween margin_top">
					<view class="ershiba nofonweight xiaohei">状态</view>
					<view class="flexright">
						<u-radio-group v-model="status" placement="row" circle>
							<u-radio activeColor="#1ba95b" :customStyle="{marginRight: '30rpx'}" label="上架"
								name="1"></u-radio>
							<u-radio activeColor="#1ba95b" label="下架" name="2"></u-radio>
						</u-radio-group>
					</view>
				</view>
				<view class="xiahuaxian flexbetween margin_top"  @click="messageTime.timeShow = true;timevalue=1">
					<view class="ershiba nofonweight xiaohei">开始时间</view>
					<view class="flexright">
						<input class="textright" type="text" placeholder="请选择" disabled="" v-model="start_time">
						<u-icon name="arrow-right"></u-icon>
					</view>
				</view>
				<view class="xiahuaxian flexbetween margin_top" @click="messageTime.timeShow = true;timevalue=2">
					<view class="ershiba nofonweight xiaohei">结束时间</view>
					<view class="flexright">
						<input class="textright" type="text" placeholder="请选择" disabled="" v-model="end_time">
						<u-icon name="arrow-right"></u-icon>
					</view>
				</view>
				<view class="xiahuaxian flexbetween margin_top">
					<view class="ershiba nofonweight xiaohei">权重</view>
					<input class="textright" type="number" placeholder="输入权重" v-model="weigh">
				</view>
				<view class="xiahuaxian flexbetween margin_top">
					<view class="ershiba nofonweight xiaohei">库存</view>
					<input class="textright" type="number" placeholder="输入库存" v-model="stock">
				</view>
				<view class="xiahuaxian flexbetween margin_top">
					<view class="ershiba nofonweight xiaohei">原价</view>
					<input class="textright" type="digit" placeholder="输入原价" v-model="yj_price">
				</view>
			</view>
		</view>
		<view class="gudingdb ffffff mainpadding">
			<view class="bigbtn" @click="submit">保存</view>
		</view>
		<timePicker :messageTime="messageTime" @timeConfirm="timeConfirm"></timePicker>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	import citys from "@/static/city/city.js"
	export default {
		data() {
			return {
				show: false,
				image: "",
				image_text: "",
				images: [],
				images_text: [],
				name: "",
				content_images: [],
				content_images_text: [],
				user_zbfapply_listid:"",
				price:"",
				status:"1",
				weigh:"",
				stock:"",
				yj_price:"",
				timevalue:0,//1开始,2结束
				messageTime:{
					timeShow:false,
					title:"选择时间",
					mode:"datetime"//date日期，datetime年月日时分秒，time时间
				},
				start_time:"",
				end_time:"",
			}
		},
		onLoad(options) {
			this.user_zbfapply_listid=options.user_zbfapply_listid
			if (options.id) {
				uni.setNavigationBarTitle({
					title: "编辑散场"
				})
				this.listid = options.id
				this.getDetail()
			}
		},
		methods: {
			timeConfirm(e){
				if(this.timevalue ==1)this.start_time = e
				if(this.timevalue ==2)this.end_time = e
				this.messageTime.timeShow = false
			},
			getDetail() {
				httpRequest.request('/api/cgcl/cgScDetail', 'GET', {
					id: this.listid,
					user_zbfapply_id:this.user_zbfapply_listid
				}).then(res => {
					let data = res.data
					this.image = data.image
					this.image_text = data.image_text
					this.images = data.images.split(",")
					this.images_text = data.images_text
					this.content_images = data.content_images.split(",")
					this.content_images_text = data.content_images_text
					this.name = data.name
					this.status = data.status
					this.price = data.price
					this.weigh = data.weigh
					this.stock = data.stock
					this.yj_price = data.yj_price
					this.start_time = data.start_time_text
					this.end_time = data.end_time_text
				})
			},
			submit() {
				if (this.image == "") {
					httpRequest.toast("请上传散场主图")
					return false
				}
				if (!this.images.length) {
					httpRequest.toast("请上传散场轮播图")
					return false
				}
				if (this.name == "") {
					httpRequest.toast("请输入散场名称")
					return false
				}
				if (!this.content_images.length) {
					httpRequest.toast("请上传散场详情图")
					return false
				}
				if (this.price == "") {
					httpRequest.toast("请输入散场价格")
					return false
				}
				if (this.start_time == "") {
					httpRequest.toast("请选择开始时间")
					return false
				}
				if (this.end_time == "") {
					httpRequest.toast("请选择结束时间")
					return false
				}
				if (this.stock == "") {
					httpRequest.toast("请输入散场库存")
					return false
				}
				if (this.yj_price == "") {
					httpRequest.toast("请输入散场原价")
					return false
				}
				uni.showLoading({
					mask:true,
					title:"请稍后"
				})
				let url = "/api/cgcl/addCgSc"
				let data = {
					image: this.image,
					images: this.images.join(),
					content_images: this.content_images.join(),
					name: this.name,
					price:this.price,
					status:this.status,
					weigh:this.weigh,
					stock:this.stock,
					yj_price:this.yj_price,
					start_time: this.start_time,
					end_time: this.end_time,
					user_zbfapply_id:this.user_zbfapply_listid
				}
				if (this.listid) {
					url = "/api/cgcl/editCgSc"
					this.$set(data, "id", this.listid)
				}
				httpRequest.request(url, 'GET', data).then(res => {
					uni.hideLoading()
					httpRequest.toast(res.msg)
					if (res.code == 1) {
						setTimeout(() => {
							uni.navigateBack(1)
						}, 1000)
					}
				})
			},
			
			uploadImg(value, upImgs, upImgs_text) { //上传图片
				let _this = this;
				this.upvalue = value
				let num = 0
				if (value == 5) {
					num = Number(value) - upImgs.length;
					if (num <= 0) {
						return false;
					}
				} else {
					num = 1
				}
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(num, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(num, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			delindex(index, upImg, upImgs_text) {
				upImg.splice(index, 1)
				upImgs_text.splice(index, 1)
			},
			uploadImageService(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					if (this.upvalue == 1) {
						this.image = res.url
						this.image_text = image_item
						return false
					}
					upImg.push(res.url)
					upImgs_text.push(image_item);
				}).catch(err => {});
			},
			upvideo() { //上传视频
				let uploadFile = ''
				let _this = this;
				// 1.选择要上传的视频
				uni.chooseVideo({
					maxDuration: 60, // 拍摄视频最长拍摄时间，单位秒。最长支持 60 秒。
					sourceType: ['album', 'camera'], // album 从相册选视频，camera 使用相机拍摄，默认为：['album', 'camera']
					success: function(res) {
						_this.uploadImageServicet(res.tempFilePath)
					}
				});
			},
			uploadImageServicet(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					this.hdjs_video = res.url
					this.hdjs_video_text = image_item
				}).catch(err => {});
			},
			delvideo() {
				this.hdjs_video = ""
				this.hdjs_video_text = ""
			}
		}
	}
</script>

<style lang="scss" scoped>
	.touxiang {
		width: 60rpx;
		height: 60rpx;
		border-radius: 50%;

		image {
			border-radius: 50%;
			width: 100%;
			height: 100%;
		}
	}

	.dizneo {
		height: 500rpx;
		overflow: auto;
	}

	.xiaolbtn {
		width: 112rpx;
		height: 48rpx;
		background: rgba(27, 169, 91, 0.1);
		border-radius: 24rpx 24rpx 24rpx 24rpx;
		font-size: 24rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #1BA95B;
		border: 2rpx solid #1BA95B;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	page {

		background: #F9F9F9;
	}

	.shanchu {
		position: absolute;
		top: -10rpx;
		right: -10rpx;
	}

	.shanchu2 {
		position: absolute;
		bottom: -10rpx;
		right: -50%;
		transform: translate(0, -50%);
	}

	.shilitu {
		width: 160rpx;
		height: 160rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.shangc {
		width: 160rpx;
		height: 160rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}
</style>